/*
 * 文件名：NumberSort.java
 * 描述： NumberSort.java
 * 修改人：Lo&Wang
 * 修改时间：2016年11月17日
 * 修改内容：新增
 */
package com.lowang.test.sort;

import com.alibaba.fastjson.JSON;

//Java实现的排序类  
public class NumberSort {
    public static void main(String[] args) {
        int[] arr = new int[]{3, 7, 1, 5, 2};
        String arrStr = JSON.toJSONString(arr);
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.bubbleSort(arr);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.quickSort(arr, 0, 4);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.quickSort2(arr, 0, 4);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.quickSort3(arr, 0, 4);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.quickSort4(arr, 0, 4);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.selectSort(arr);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.selectSort2(arr);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.insertSort(arr);
        System.out.println(JSON.toJSONString(arr));
        arr = JSON.parseObject(arrStr, int[].class);
        NumberSort.insert2(arr);
        System.out.println(JSON.toJSONString(arr));
    }
    //私有构造方法，禁止实例化  
    private NumberSort() {
        super();
    }
    //冒泡法排序 
    public static void bubbleSort(int[] numbers) {
        int temp; // 记录临时中间值   
        int size = numbers.length; // 数组大小   
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                if (numbers[i] > numbers[j]) { // 交换两数的位置   
                    temp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = temp;
                }
            }
        }
    }
    //快速排序
    public static void quickSort(int[] numbers, int start, int end) {
        if (start < end) {
            int base = numbers[start]; // 选定的基准值（第一个数值作为基准值）   
            int temp; // 记录临时中间值   
            int i = start, j = end;
            do {
                while ((numbers[i] < base) && (i < end))
                    i++;
                while ((numbers[j] > base) && (j > start))
                    j--;
                if (i <= j) {
                    temp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = temp;
                    i++;
                    j--;
                }
            } while (i <= j);
            if (start < j)
                quickSort(numbers, start, j);
            if (end > i)
                quickSort(numbers, i, end);
        }
    }
    //快速排序4
    public static void quickSort4(int[] numbers, int start, int end) {
        if (start < end) {
            int tmp = numbers[start];
            int l = start, r = end;
            while (l < r) {
                while (l < r && numbers[r] > tmp) {
                    r--;
                }
                if (l < r) {
                    numbers[l++] = numbers[r];
                }
                while (l < r && numbers[l] < tmp) {
                    l++;
                }
                if (l < r) {
                    numbers[r--] = numbers[l];
                }
            }
            numbers[l] = tmp;
            quickSort4(numbers, start, l - 1);
            quickSort4(numbers, l + 1, end);
        }
    }
    //快速排序2
    public static void quickSort2(int[] numbers, int start, int end) {
        if (start < end) {
            int middle = getMiddle(numbers, start, end);
            quickSort2(numbers, start, middle - 1);
            quickSort2(numbers, middle + 1, end);
        }
    }
    private static int getMiddle(int[] numbers, int start, int end) {
        int tmp = numbers[start];
        int low = start, high = end;
        while (low < high) {
            while (numbers[high] > tmp && high > start) {
                high--;
            }
            numbers[low] = numbers[high];
            numbers[high] = tmp;
            while (numbers[low] < tmp && low < end) {
                low++;
            }
            numbers[high] = numbers[low];
            numbers[low] = tmp;
        }
        return low;
    }
    public static void selectSort2(int[] numbers) {
        int size = numbers.length, tmp;
        for (int i = 0; i < size; i++) {
            int m = i;
            for (int j = size - 1; j > i; j--) {
                if (numbers[j] < numbers[m]) {
                    m = j;
                }
            }
            tmp = numbers[m];
            numbers[m] = numbers[i];
            numbers[i] = tmp;
        }
    }
    //选择排序 
    public static void selectSort(int[] numbers) {
        int size = numbers.length, temp;
        for (int i = 0; i < size; i++) {
            int k = i;
            for (int j = size - 1; j > i; j--) {
                if (numbers[j] < numbers[k])
                    k = j;
            }
            temp = numbers[i];
            numbers[i] = numbers[k];
            numbers[k] = temp;
        }
    }
    public static void insert2(int[] arr) {
        //插入排序，把第一个元素当做已排序，取下一个元素，和已排序的序列中插入适合位置.
        int size = arr.length;
        int tmp, j;
        for (int i = 1; i < size; i++) {
            tmp = arr[i];
            for (j = i; j > 0; j--) {
                if (arr[j - 1] > tmp) {
                    arr[j] = arr[j - 1];
                } else {
                    break;
                }
            }
            arr[j] = tmp;
        }
    }
    //插入排序    
    // @param numbers  
    public static void insertSort(int[] numbers) {
        int size = numbers.length, temp, j;
        for (int i = 1; i < size; i++) {
            temp = numbers[i];
            for (j = i; j > 0 && temp < numbers[j - 1]; j--)
                numbers[j] = numbers[j - 1];
            numbers[j] = temp;
        }
    }
    //归并排序  
    public static void mergeSort(int[] numbers, int left, int right) {
        int t = 1;// 每组元素个数   
        int size = right - left + 1;
        while (t < size) {
            int s = t;// 本次循环每组元素个数   
            t = 2 * s;
            int i = left;
            while (i + (t - 1) < size) {
                merge(numbers, i, i + (s - 1), i + (t - 1));
                i += t;
            }
            if (i + (s - 1) < right)
                merge(numbers, i, i + (s - 1), right);
        }
    }
    //归并算法实现  
    private static void merge(int[] data, int p, int q, int r) {
        int[] B = new int[data.length];
        int s = p;
        int t = q + 1;
        int k = p;
        while (s <= q && t <= r) {
            if (data[s] <= data[t]) {
                B[k] = data[s];
                s++;
            } else {
                B[k] = data[t];
                t++;
            }
            k++;
        }
        if (s == q + 1)
            B[k++] = data[t++];
        else
            B[k++] = data[s++];
        for (int i = p; i <= r; i++)
            data[i] = B[i];
    }
    public static void quickSort3(int[] a, int left, int right) {
        if (left < right) {
            int pivot = partition(a, left, right);
            quickSort(a, left, pivot - 1);
            quickSort(a, pivot + 1, right);
        }
        System.out.println("quickSort3");
    }
    private static int partition(int[] a, int left, int right) {
        int pivot = a[left];
        while (left < right) {
            while (left < right && pivot <= a[right])
                --right;
            a[left] = a[right];
            while (left < right && pivot >= a[left])
                ++left;
            a[right] = a[left];
        }
        a[left] = pivot;
        return left;
    }
}
